/*
* 产品SOP关系表模块
*/
const express = require('express');
const router = express.Router();
// 引入数据库
const db = require("../../../config/db");
// 引入时间包
const moment = require("moment");
// 引入方法库
const { toCamelCase, toUnderlineCase, verifyToken} = require("../../../config/utils");
// 引入日志
const log = require("../../log");

// 获取产品SOP关系表
router.get("/list", (req, res) => {
    console.log("===========查询获取产品SOP关系表接口============");
    // 获取参数
    let {pageNum, pageSize, itemId} = req.query;
    pageNum = pageNum || 1;
    pageSize = pageSize || 10;
    let sql = `select * from md_product_sop where item_id = '${itemId}'`;
    // 分页查询
    sql += ` limit ${(pageNum - 1) * pageSize}, ${pageSize}`;
    // 查询数据库
    db.query("查询获取产品SOP关系表", sql, (err, result) => {
        if (err) {
            res.send({code: 500, msg: "查询获取产品SOP关系表失败"})   
            return
        }
        // 查询总数
        let sqlCount = `select count(*) as count from md_product_sop where item_id = '${itemId}'`;
        db.query("查询获取产品SOP关系表总数", sqlCount, (err, resultCount) => {
            if (err) {
                res.send({code: 500, msg: "查询获取产品SOP关系表总数失败"})
                return
            }
            if (result.length > 0) {
                 // 变量Json数组key转成下划线第一个大写
                 result = result.map(item => {
                    return toCamelCase(item)
                })
            }
            res.send({code: 200, msg: "查询获取产品SOP关系表成功", rows: result, total: resultCount[0].count})
        })
    })
})

// 新增产品SOP关系表
router.post("/", (req, res) => {
    console.log("===========新增产品SOP关系表表接口============");
    // 获取参数
    let {itemId, orderNum, sopTitle, sopDescription, processId, sopUrl} = req.body;
    // 判断标题是否为空--后续增加工序后，增加判断
    if(!sopTitle){
        res.send({code: 500, msg: "请输入SOP标题"})
        return
    }
    // 获取创建人
    let {username} = verifyToken(req.headers.authorization);
    // 获取创建时间
    let createTime = moment().format("YYYY-MM-DD HH:mm:ss");
    // 拼接sql
    let sql = `insert into md_product_sop (item_id, order_num, sop_title, sop_description, process_id, sop_url, create_time, create_by)
    values 
    ('${itemId}', '${orderNum}', '${sopTitle}',  ${sopDescription ? "'"+sopDescription+"'" : null}, 
    ${processId ? "'"+processId+"'" : null}, ${sopUrl ? "'"+sopUrl+"'" : null}, '${createTime}', '${username}')`;
    // 查询数据库
    db.query("新增产品SOP关系表", sql, (err, result) => {
        if (err) {
            res.send({code: 500, msg: "新增产品SOP关系表失败"})
            return
        }
        if(result.affectedRows > 0){
            res.send({code: 200, msg: "新增产品SOP关系表成功"})
        }else{
            res.send({code: 500, msg: "新增产品SOP关系表失败"})
        }
    })
})

// 查询单个产品SOP关系表
router.get("/:id", (req, res) => {
    console.log("===========查询单个产品SOP关系表接口============");
    // 获取参数
    let {id} = req.params;
    // 拼接sql
    let sql = `select * from md_product_sop where sop_id = '${id}'`;
    // 查询数据库
    db.query("查询单个产品SOP关系表", sql, (err, result) => {
        if (err) {
            res.send({code: 500, msg: "查询单个产品SOP关系表失败"})
            return
        }
        if (result.length > 0) {
            // 变量Json数组key转成下划线第一个大写
            result = result.map(item => {
                return toCamelCase(item)
            })
        }
        res.send({code: 200, msg: "查询单个产品SOP关系表成功", data: result[0]})
    })
})

// 修改产品SOP关系表
router.put("/", (req, res) => {
    console.log("===========修改产品SOP关系表接口============");
    // 获取参数
    let {sopId, orderNum, sopTitle, sopDescription, processId, sopUrl} = req.body;
    // 判断标题是否为空--后续增加工序后，增加判断
    if(!sopTitle){
        res.send({code: 500, msg: "请输入SOP标题"})
        return
    }
    // 获取创建人
    let {username} = verifyToken(req.headers.authorization);
    // 获取创建时间
    let updateTime = moment().format("YYYY-MM-DD HH:mm:ss");
    // 拼接sql
    let sql = `update md_product_sop set order_num = '${orderNum}', sop_title = '${sopTitle}',
    sop_description = ${sopDescription ? "'"+sopDescription+"'" : null}, process_id = ${processId ? "'"+processId+"'" : null},
    sop_url = ${sopUrl ? "'"+sopUrl+"'" : null}, update_time = '${updateTime}', update_by = '${username}' where sop_id = '${sopId}'`;
    // 查询数据库
    db.query("修改产品SOP关系表", sql, (err, result) => {
        if (err) {
            res.send({code: 500, msg: "修改产品SOP关系表失败"})
            return
        }
        if(result.affectedRows > 0){
            res.send({code: 200, msg: "修改产品SOP关系表成功"})
        }else{
            res.send({code: 500, msg: "修改产品SOP关系表失败"})
        }
    })
})

// 删除产品SOP关系表
router.delete("/:id", (req, res) => {
    console.log("===========删除产品SOP关系表接口============");
    // 获取参数
    let {id} = req.params;
    // 拼接sql
    let sql = `delete from md_product_sop where sop_id = '${id}'`;
    // 查询数据库
    db.query("删除产品SOP关系表", sql, (err, result) => {
        if (err) {
            res.send({code: 500, msg: "删除产品SOP关系表失败"})
            return
        }
        if(result.affectedRows > 0){
            res.send({code: 200, msg: "删除产品SOP关系表成功"})
        }else{
            res.send({code: 500, msg: "删除产品SOP关系表失败"})
        }
    })
})

module.exports = router;